Passed
Push — master ( fe7b3f...22651e )
by Andrey
08:30
created

CKEDITOR.plugins.add(ꞌpbckcodeꞌ).init   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 82
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 43
nc 4
nop 1
dl 0
loc 82
c 0
b 0
f 0
cc 3
rs 8.8478

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
// needed js files
2
var js = {
3
  ace: 'ace.js',
4
  aceExtWhitespace: 'ext-whitespace.js',
5
  pbSyntaxHighlighter: CKEDITOR.plugins.getPath('pbckcode') + 'dialogs/PBSyntaxHighlighter.js'
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
};
7
8
var commandName = 'pbckcode';
9
10
/**
11
 * Plugin definition
12
 */
13
CKEDITOR.plugins.add('pbckcode', {
14
  icons: 'pbckcode',
15
  hidpi: true,
16
  lang: ['fr', 'en', 'ru'],
17
  init: function(editor) {
18
    // if there is no user settings
19
    // create an empty object
20
    if (editor.config.pbckcode === undefined) {
21
      editor.config.pbckcode = {};
22
    }
23
24
    // default settings object
25
    var DEFAULT_SETTINGS = {
26
      cls: '',
27
      modes: [
28
        ['HTML', 'html'],
29
        ['CSS', 'css'],
30
        ['PHP', 'php'],
31
        ['JS', 'javascript']
32
      ],
33
      theme: 'textmate',
34
      tab_size: 4,
35
      js: '//cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/'
36
    };
37
38
    // merge user settings with default settings
39
    editor.settings = CKEDITOR.tools.extend(DEFAULT_SETTINGS, editor.config.pbckcode, true);
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
40
    editor.settings.js = normalizeJsUrl(editor.settings.js);
41
42
    // load CSS for the dialog
43
    editor.on('instanceReady', function() {
44
      CKEDITOR.document.appendStyleSheet(this.path + 'dialogs/style.css');
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
45
    }.bind(this));
46
47
    // add the button in the toolbar
48
    editor.ui.addButton('pbckcode', {
49
      label: editor.lang.pbckcode.addCode,
50
      command: commandName,
51
      toolbar: 'pbckcode'
52
    });
53
54
    // link the button to the command
55
    editor.addCommand(commandName, new CKEDITOR.dialogCommand('pbckcodeDialog', {
56
        allowedContent: 'pre[*]{*}(*)'
57
      })
58
    );
59
60
    // disable the button while the required js files are not loaded
61
    editor.getCommand(commandName).disable();
62
63
    // add the plugin dialog element to the plugin
64
    CKEDITOR.dialog.add('pbckcodeDialog', this.path + 'dialogs/pbckcode.js');
65
66
    // add the context menu
67
    if (editor.contextMenu) {
68
      editor.addMenuGroup('pbckcodeGroup');
69
      editor.addMenuItem('pbckcodeItem', {
70
        label: editor.lang.pbckcode.editCode,
71
        icon: this.path + 'icons/pbckcode.png',
72
        command: commandName,
73
        group: 'pbckcodeGroup'
74
      });
75
76
      editor.contextMenu.addListener(function(element) {
77
        if (element.getAscendant('pre', true)) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if element.getAscendant("pre", true) is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
78
          return {pbckcodeItem: CKEDITOR.TRISTATE_OFF};
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
79
        }
80
      });
81
    }
82
83
    var scripts = [
84
      getScriptUrl(editor.settings.js, js.ace),
85
      js.pbSyntaxHighlighter
86
    ];
87
88
    // Load the required js files
89
    // enable the button when loaded
90
    CKEDITOR.scriptLoader.load(scripts, function() {
91
      editor.getCommand(commandName).enable();
92
93
      // need ace to be loaded
94
      CKEDITOR.scriptLoader.load([
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
95
        getScriptUrl(editor.settings.js, js.aceExtWhitespace)
96
      ]);
97
    });
98
  }
99
});
100
101
function normalizeJsUrl(js) {
102
  return js.concat('/')
103
    .replace(new RegExp('([^:]\/)\/+', 'g'), '$1');
104
}
105
106
function getScriptUrl(prefix, scriptName) {
107
  return prefix + scriptName;
108
}
109
110